{{#section 'style'}}<link rel="stylesheet" href="/style/center.css">{{/section}}

<div class="techbook history">
    <!--head-->
    {{> header_center}}
    <!--head End-->

    <!--body-->
    <div class="body">
        <div class="wrap">
            <h3 class="title">浏览历史</h3>

            <div class="historyBox" id="historyBox">

                {{#each history as |h|}}

                <dl class="day">
                    <dt><i></i>{{#if this.day}}<em>{{this.day}}</em>{{/if}}<p>{{this.date}}</p><span data-date="{{this.date}}">删除</span></dt>
                    <dd>
                        <div class="historyList">
                            {{#each this.books}}
                            <dl>
                                <dt><a href="/item/{{this.sku}}"><img src="/uploads/previews/{{this.img}}" alt=""/></a></dt>
                                <dd>
                                    <div><b>￥{{this.price}}</b>&nbsp;<s>￥{{this.market}}</s></div>
                                    <span>加入购物车</span>
                                    <i data-book_id="{{this._id}}"></i>
                                </dd>
                            </dl>
                            {{/each}}
                        </div>
                        <div class="cb"></div>
                    </dd>
                </dl>

                {{/each}}

            </div>


            <div class="loading">加载中，请稍候...</div>
            <div class="noMore"><p>已到最后，只保存最后一个月的浏览记录</p></div>

        </div>
    </div>
    <!--body End-->

    <!--foot-->
    {{>footer}}
    <!--foot End-->

    <!--sideBar-->

    <!--sideBar End-->

</div>

{{#section 'script'}}
<script>
    $(function(){
        var $list=$('.historyList');
        //单个浏览记录删除
        $list.on('click','i', function () {
            var $dl=$(this).closest('dl'),$p=$(this).closest('.day'),length=$(this).closest('.historyList').find('dl').length;
            if(length>1){//删除当前足迹
                $dl.hide(200, function () {
                    $dl.remove();
                });
                $.ajax({
                    url:'history',
                    type:'DELETE',
                    data:{
                        user_id:'{{user._id}}',
                        book_id:$(this).data('book_id')
                    },
                    success: function (ret) {
                        console.log('删除成功：',ret);
                    },
                    error: function (e) {
                        console.log('删除失败：',e);
                    }
                });

            }else{//删除当天足迹
                $p.children('dt').find('span').trigger('click');
            }
        });

        //当天全部浏览记录删除
        $('.day>dt').on('click','span',function () {
            $(this).closest('.day').hide(200, function () {
                $(this).remove();
            });
            $.ajax({
                url:'history',
                type:'DELETE',
                data:{
                    user_id:'{{user._id}}',
                    date:$(this).data('date')
                },
                success: function (ret) {
                    console.log('删除成功：',ret);
                },
                error: function (e) {
                    console.log('删除失败：',e);
                }
            });
        });

    });
</script>
{{/section}}